var app = getApp();
var utils = require("../../utils/util.js");
Page({
  data: {
    //是否已经选择视频
    isChooseVideo: false,
    //视频资源
    videoResource: null,
    //bgm
    bgm: null,
    //分享心情
    describe: '',
    label: null,
    //弹窗提示
    toasthidden: true,
    toastTitle: '提示',
    toastContent: '您选择的视频已经超过15秒啦, 请重新选择',
    toastConfirm: '确定',
  },

  onLoad: function() {
    //找到系统状态位置
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15
    })
    this.getCookie();
  },

  //选择视频
  chooseVideo: function() {
    var isChooseVideo = this.data.isChooseVideo;
    //如果是重新选择视频,需要清除上次的全部状态
    if (isChooseVideo) {
      this.setData({
        //是否已经选择视频
        isChooseVideo: false,
        //视频资源
        videoResource: null,
      })
    }
    var that = this;
    wx.chooseVideo({
      maxDuration: 15,
      success: function(res) {
        var duration = parseInt(res.duration);
        //如果视频大于5分钟,提示超过最长限制
        if (duration > 15) {
          that.setData({
            toasthidden: false
          })
        } else {
          that.setData({
            videoResource: res,
            isChooseVideo: true
          })
        }
      }
    })
  },
  //关闭弹窗
  toastConfirm: function() {
    this.setData({
      toasthidden: true
    })
  },
  //输入文字
  inputDescribe: function(e) {
    //当前输入数量
    var cursor = e.detail.cursor;
    var value = e.detail.value;
    //不能超过500
    if (cursor > 1000) {
      this.setData({
        describe: value.substring(0, 1000)
      })
    } else {
      this.setData({
        describe: value
      })
    }
  },
  //选择话题
  selectLable: function() {
    wx.navigateTo({
      url: '/pages/label/label',
    })
  },
  //存草稿
  cookie: function() {
    var video = this.data.videoResource;
    var label = this.data.label;
    var describe = this.data.describe;
    var bgm = this.data.bgm
    //如果都为空,则不存
    if (video == null && label == null && describe == '' && bgm == null) {
      return;
    }
    var videoTalk = {
      video: video,
      label: label,
      describe: describe,
      bgm: bgm
    }
    wx.setStorage({
      key: 'videoTalk',
      data: JSON.stringify(videoTalk),
    })
    wx.showToast({
      title: '已帮您成功存稿',
      icon: 'none',
      duration: 3000
    })
  },
  //从缓存中取出数据渲染
  getCookie: function() {
    var videoTalkStr = wx.getStorageSync("videoTalk");
    //如果有缓存
    if (videoTalkStr != '') {
      var videoTalk = JSON.parse(videoTalkStr);
      var that = this;
      wx.showModal({
        title: '提示',
        content: '是否继续编写保存的草稿',
        cancelText: '重写',
        cancelColor: '#000000',
        confirmText: '确定',
        confirmColor: '#627096',
        success: function(e) {
          if (e.confirm) {
            var describe = videoTalk.describe;
            var label = videoTalk.label;
            var video = videoTalk.video
            var bgm = videoTalk.bgm

            //设置话题 设置bgm 设置心情
            that.setData({
              videoResource: video,
              isChooseVideo: video == null ? false : true,
              describe: describe,
              label: label,
              bgm: bgm
            })
          }
          if (e.cancel) {
            that.playError();
          }
        }
      })


    }
  },
  //播放错误.清除缓存
  playError: function(e) {
    this.setData({
      label: null,
      bgm: null,
      videoResource: null,
      describe: ''
    })
    wx.removeStorage({
      key: 'videoTalk',
    })
  },

  //添加音乐
  addBgm: function() {
    wx.navigateTo({
      url: '/pages/videoBack/videoBack'
    })
  },
  //存草稿
  cookie: function() {
    var video = this.data.videoResource;
    var label = this.data.label;
    var describe = this.data.describe;
    var bgm = this.data.bgm
    //如果都为空,则不存
    if (video == null && label == null && describe == '' && bgm == null) {
      return;
    }
    var videoTalk = {
      video: video,
      label: label,
      describe: describe,
      bgm: bgm
    }
    wx.setStorage({
      key: 'videoTalk',
      data: JSON.stringify(videoTalk),
    })
    wx.showToast({
      title: '已帮您成功存稿',
      icon: 'none',
      duration: 3000
    })
  },

  //返回下一个页面
  rollback: function() {
    //获取页面值栈
    wx.navigateBack({
      delta: 1
    })
  },

  //发布
  addTalk: function() {
    var that = this;
    var videoResource = this.data.videoResource;
    var label = this.data.label;
    var describe = this.data.describe;
    var bgm = this.data.bgm;
    if (videoResource == null) {
      wx.showToast({
        title: '您还没有上传视频呢.',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    if (describe == '') {
      wx.showToast({
        title: '您还没有分享心情呢.',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    wx.showToast({
      title: '正在努力发布.',
      icon: 'loading',
      duration: 3000,
      mask: true
    })
    //延迟跳转
    var timeout = setTimeout(function() {
      that.rollback();
      clearTimeout(timeout);
    }, 3000)
    wx.request({
      url: app.globalData.requestUrl + 'getUploadFileOssPolicy',
      method: 'GET',
      data: {
        userId: app.globalData.user.id,
        type: 2
      },
      success: function(r) {
        if (r.data.code == 200) {
          //生成名称
          var uuid = utils.getUUID();
          //获取类型
          var mimeType = utils.getMimeType(videoResource.tempFilePath);
          var src = uuid + mimeType
          wx.uploadFile({
            url: r.data.data.host,
            filePath: videoResource.tempFilePath,
            name: 'file',
            formData: {
              key: src,
              policy: r.data.data.policy,
              OSSAccessKeyId: r.data.data.accessid,
              success_action_status: '200',
              signature: r.data.data.signature
            },
            success: function(r) {
              var resourceList = [{
                src: src,
                sequence: 1,
                width: videoResource.width,
                height: videoResource.height,
                size: videoResource.duration
              }];
              var data = {
                userId: app.globalData.user.id,
                talkDescribe: describe,
                label: label == null ? '' : label.content,
                talkType: bgm == null ? 1 : 2,
                bgmId: bgm == null ? '' : bgm.id,
                bgmAuthor: bgm == null ? '' : bgm.author,
                bgmName: bgm == null ? '' : bgm.name,
                bgmSrc: bgm == null ? '' : bgm.src,
                resourceList: resourceList,
              }
              wx.request({
                url: app.globalData.requestUrl + 'addTalk',
                method: 'POST',
                data: data,
                success: function(r) {
                  if (r.data.code == 200) {
                    wx.removeStorage({
                      key: 'videoTalk'
                    })
                  } else {
                    that.errCookie();
                  }
                },
                fail: function() {
                  that.errCookie();
                }
              })
            },
            fail: function() {
              that.errCookie();
            },
          })
        } else {
          that.errCookie();
        }
      },
      fail: function() {
        that.errCookie();
      }
    })
  },
  //发布失败存草稿
  errCookie: function() {
    var video = this.data.videoResource;
    var label = this.data.label;
    var describe = this.data.describe;
    var bgm = this.data.bgm
    var videoTalk = {
      video: video,
      label: label,
      describe: describe,
      bgm: bgm
    }
    wx.setStorage({
      key: 'videoTalk',
      data: JSON.stringify(videoTalk),
    })
    wx.showToast({
      title: '发布失败,已帮您自动存稿',
      icon: 'none',
      duration: 3000
    })
  },

})